﻿using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CodeCube.Common;
using SqlSugar;

namespace CodeCube.Core.Extensions
{
    public static class QueryableExtension
    {
        /// <summary>
        /// 读取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="isOrderBy"></param>
        /// <returns></returns>
        public static async Task<Page<T>> ToPageAsync<T>(this ISugarQueryable<T> query,
            int pageIndex,
            int pageSize,
            bool isOrderBy = false)
        {
            RefAsync<int> totalItems =0;
            var page = new Page<T>();
            page.Items = await query.ToPageListAsync(pageIndex, pageSize, totalItems);
            var totalPages = totalItems != 0 ? (totalItems % pageSize) == 0 ? (totalItems / pageSize) : (totalItems / pageSize) + 1 : 0;
            page.CurrentPage = pageIndex;
            page.ItemsPerPage = pageSize;
            page.TotalItems = totalItems;
            page.TotalPages = totalPages;
            return page;
        }

        /// <summary>
        /// 读取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="isOrderBy"></param>
        /// <returns></returns>
        public static Page<T> ToPage<T>(this ISugarQueryable<T> query,
            int pageIndex,
            int pageSize,
            bool isOrderBy = false)
        {
            var page = new Page<T>();
            var totalItems = 0;
            page.Items = query.ToPageList(pageIndex, pageSize, ref totalItems);
            var totalPages = totalItems != 0 ? (totalItems % pageSize) == 0 ? (totalItems / pageSize) : (totalItems / pageSize) + 1 : 0;
            page.CurrentPage = pageIndex;
            page.ItemsPerPage = pageSize;
            page.TotalItems = totalItems;
            page.TotalPages = totalPages;
            return page;
        }
        public static async Task<KTDataTable<T>> ToKTDataTableAsync<T>(this ISugarQueryable<T> query,
            int pageIndex,
            int pageSize,
            string field,
            string sort)
        {
            var ktdt = new KTDataTable<T>();
            var totalItems = 0;
            pageSize = pageSize < 0 ? 20 : pageSize;
            
            ktdt.data = await query.OrderBy(field+" "+sort).ToPageListAsync(pageIndex, pageSize, totalItems);
            totalItems = query.Count();
            var totalPages = totalItems != 0 ? (totalItems % pageSize) == 0 ? (totalItems / pageSize) : (totalItems / pageSize) + 1 : 1;
            ktdt.meta = new KTDataTableMeta { page = pageIndex, perpage = pageSize, pages = totalPages, total = totalItems, sort = sort, field = field };
            return ktdt;
        }
        public static async Task<KTDataTable<TResult>> ToKTDataTableAsync<T,T2,TResult>(
            this ISugarQueryable<T,T2> query, 
            Expression<Func<T, T2, TResult>> expression, 
            int pageIndex,
            int pageSize) 
        {
            var ktdt = new KTDataTable<TResult>();
            var totalItems = 0;
            pageSize = pageSize < 0 ? 20 : pageSize;

            ktdt.data = await query.Select(expression).ToPageListAsync(pageIndex, pageSize, totalItems);
            totalItems = query.Count();
            var totalPages = totalItems != 0 ? (totalItems % pageSize) == 0 ? (totalItems / pageSize) : (totalItems / pageSize) + 1 : 1;
            ktdt.meta = new KTDataTableMeta { page = pageIndex, perpage = pageSize, pages = totalPages, total = totalItems };
            return ktdt;
        }
        public static async Task<KTDataTable<T>> ToKTDataTableAsync<T>(this ISugarQueryable<T> query,int pageIndex,int pageSize)
        {
            var ktdt = new KTDataTable<T>();
            var totalItems = 0;
            pageSize = pageSize < 0 ? 20 : pageSize;
            ktdt.data = await query.ToPageListAsync(pageIndex, pageSize, totalItems);
            totalItems = query.Count();
            var totalPages = totalItems != 0 ? (totalItems % pageSize) == 0 ? (totalItems / pageSize) : (totalItems / pageSize) + 1 : 1;
            ktdt.meta = new KTDataTableMeta { page = pageIndex, perpage = pageSize, pages = totalPages, total = totalItems };
            return ktdt;
        }

        public static KTDataTable<T> ToKTDataTable<T>(this ISugarQueryable<T> query,
            int pageIndex,
            int pageSize,
            string field,
            string sort)
        {
            var ktdt = new KTDataTable<T>();
            var totalItems = 0;
            pageSize = pageSize < 0 ? 20 : pageSize;
            ktdt.data= query.OrderBy(field + " " + sort).ToPageList(pageIndex, pageSize, ref totalItems);
            var totalPages = totalItems != 0 ? (totalItems % pageSize) == 0 ? (totalItems / pageSize) : (totalItems / pageSize) + 1 : 1;
            ktdt.meta = new KTDataTableMeta { page = pageIndex, perpage = pageSize, pages = totalPages, total = totalItems, sort = sort, field = field };
            return ktdt;
        }
        public static KTDataTable<T> ToKTDataTable<T>(this ISugarQueryable<T> query, int pageIndex, int pageSize)
        {
            var ktdt = new KTDataTable<T>();
            var totalItems = 0;
            pageSize = pageSize < 0 ? 20 : pageSize;

            ktdt.data = query.ToPageList(pageIndex, pageSize,ref totalItems);
            var totalPages = totalItems != 0 ? (totalItems % pageSize) == 0 ? (totalItems / pageSize) : (totalItems / pageSize) + 1 : 1;
            ktdt.meta = new KTDataTableMeta { page = pageIndex, perpage = pageSize, pages = totalPages, total = totalItems,};
            return ktdt;
        }
    }
}
